<?php
namespace app\forum\model;
use app\base\model\BaseModel;
/**
 * 积分操作
 */
class IntegralModel extends BaseModel {

    /**
     * 获取列表
     * @return array 列表
     */
    public function loadList($where = array(),$limit = 10 ,$order='int_id desc'){
        return $this->table("integral as A")
                    ->join('{pre}user as B ON A.user_id = B.user_id')
                    ->field('A.*,B.username')
                    ->where($where)
                    ->order($order)
                    ->limit($limit)
                    ->select();
    }

    /**
     * 获取数量
     * @return array 列表
     */
    public function countList($where = array()){
        return $this->table("integral as A")
                    ->join('{pre}user as B ON A.user_id = B.user_id')
                    ->field('A.*,B.username')
                    ->where($where)
                    ->count();
    }

    /**
     * 获取信息
     * @param int $intId ID
     * @return array 信息
     */
    public function getInfo($intId)
    {
        $map = array();
        $map['int_id'] = $intId;
        return $this->getWhereInfo($map);
    }

    /**
     * 获取信息
     * @param array $where 条件
     * @return array 信息
     */
    public function getWhereInfo($where)
    {
        return $this->where($where)->find();
    }


    /**
     * 操作积分
     * @param int $userName 用户名
     * @param int $type 积分数量
     * @param string $type 1增加 0减少
     * @param string $remarks 备注
     * @return bool 更新状态
     */
    public function settingInt($userName, $num=1, $type=1, $remarks = '暂无')
    {
        //获取用户信息
        $where = array();
        $where['username'] = $userName;
        $userInfo = target('forum/User')->getWhereInfo($where);
        if(empty($userInfo)){
            $this->error = '操作用户不存在！';
            return false;
        }
        //操作积分
        $this->beginTransaction();
        if($type){
            //增加
            $data = array();
            $cutIntegral = intval($userInfo['integral']) + $num;
            //升级用户组
            if(!$userInfo['special'] && $userInfo['status'] && !$userInfo['admin']){
                $where = array();
                $where[] = 'special <> 1 AND status = 1 AND admin <> 1 AND integral_limit <= '.$cutIntegral;
                $groupList = target('forum/UserGroup')->loadList($where,'integral_limit desc');
                $upGroup = $groupList[0];
                if($upGroup && $cutIntegral > $upGroup['integral_limit'] && $upGroup['group_id'] <> $userInfo['group_id']){
                    $data['group_id'] = $upGroup['group_id'];
                }
            }
            $data['integral'] = $cutIntegral;
            $where = array();
            $where['user_id'] = $userInfo['user_id'];
            $status = target('forum/User')->data($data)->where($where)->update();
            if($status === false){
                $this->rollBack();
                return false;
            }
        }else{
            //减少
            if($userInfo['integral'] < $num){
                $this->error = '您要扣除的积分大于当前用户的积分！';
                return false;
            }
            $where = array();
            $where['user_id'] = $userInfo['user_id'];
            $status = target('forum/User')->where($where)->setDec('integral',$num);
            if($status === false){
                $this->rollBack();
                return false;
            }
        }
        //添加记录
        $data = array();
        $data['type'] = $type;
        $data['num'] = $num;
        $data['time'] = time();
        $data['user_id'] = $userInfo['user_id'];
        $data['remarks'] = $remarks;
        if($this->add($data)){
            $this->commit();
            return true;
        }else{
            $this->rollBack();
            return false;
        }
    }

    /**
     * 删除信息
     * @param int $intId ID
     * @return bool 删除状态
     */
    public function delData($intId)
    {
        $map = array();
        $map['int_id'] = $intId;
        return $this->where($map)->delete();
    }

}
